package com.meiyu.live.ui.main.widget;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import com.meiyu.live.BaseActivity;
import com.meiyu.live.R;

import java.util.Arrays;
import java.util.List;

public class AllHotLiveDrawerView extends FrameLayout implements View.OnClickListener {

    private TextView tv_tvcount;
    private View btn_reset, btn_ok;
    private View[] btn_filter;
    private ImageView[] iv_filter;

    private FilterViewListener filterViewListener;

    private BaseActivity activity;

    private int type_position = -1;
    private List<FilterItemBean> filterList = Arrays.asList(
            new FilterItemBean(1, "游戏", R.drawable.btn_filter_n0, R.drawable.btn_filter_s0),
            new FilterItemBean(3, "娱乐", R.drawable.btn_filter_n1, R.drawable.btn_filter_s1),
            new FilterItemBean(9, "体育", R.drawable.btn_filter_n2, R.drawable.btn_filter_s2),
            new FilterItemBean(5, "户外", R.drawable.btn_filter_n3, R.drawable.btn_filter_s3),
            new FilterItemBean(10, "金融", R.drawable.btn_filter_n4, R.drawable.btn_filter_s4)
    );

    public interface FilterViewListener {
        void onClickOk(int id);
    }

    private class FilterItemBean {
        public int id;
        public String name;
        public int res_n;
        public int res_s;

        FilterItemBean(int id, String name, int res_n, int res_s) {
            this.id = id;
            this.name = name;
            this.res_n = res_n;
            this.res_s = res_s;
        }
    }

    public AllHotLiveDrawerView(Context context) {
        super(context);
        init(context);
    }

    public AllHotLiveDrawerView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    public AllHotLiveDrawerView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init(context);
    }

    private void init(Context context) {
        activity = (BaseActivity) context;
        LayoutInflater inflater = LayoutInflater.from(context);
        inflater.inflate(R.layout.view_hotlivedrawer, this);
        tv_tvcount = (TextView) findViewById(R.id.tv_tvcount);

        btn_filter = new View[]{
                findViewById(R.id.btn_filter0),
                findViewById(R.id.btn_filter1),
                findViewById(R.id.btn_filter2),
                findViewById(R.id.btn_filter3),
                findViewById(R.id.btn_filter4)
        };
        iv_filter = new ImageView[]{
                (ImageView) findViewById(R.id.iv_filter0),
                (ImageView) findViewById(R.id.iv_filter1),
                (ImageView) findViewById(R.id.iv_filter2),
                (ImageView) findViewById(R.id.iv_filter3),
                (ImageView) findViewById(R.id.iv_filter4)
        };
        for (View view : btn_filter) {
            view.setOnClickListener(onClickListener_type);
        }

        btn_reset = findViewById(R.id.btn_reset);
        btn_reset.setOnClickListener(this);
        btn_ok = findViewById(R.id.btn_ok);
        btn_ok.setOnClickListener(this);
    }

    private OnClickListener onClickListener_type = new OnClickListener() {
        @Override
        public void onClick(View view) {
            for (int i = 0; i < btn_filter.length; i++) {
                if (view == btn_filter[i]) {
                    if (type_position != i) {
                        type_position = i;
                        iv_filter[i].setImageResource(filterList.get(i).res_s);
                    } else {
                        type_position = -1;
                        iv_filter[i].setImageResource(filterList.get(i).res_n);
                    }
                } else {
                    iv_filter[i].setImageResource(filterList.get(i).res_n);
                }
                getData();
            }
        }
    };

    public void setFilterViewListener(FilterViewListener filterViewListener) {
        this.filterViewListener = filterViewListener;
    }

    @Override
    public void onClick(View view) {
        if (view == btn_reset) {
            //重置标签
            type_position = -1;
            for (int i = 0; i < btn_filter.length; i++) {
                iv_filter[i].setImageResource(filterList.get(i).res_n);
            }
        } else if (view == btn_ok) {
            if (filterViewListener != null) {
                if (type_position >= 0) {
                    filterViewListener.onClickOk(filterList.get(type_position).id);
                } else {
                    filterViewListener.onClickOk(-1);
                }
            }
        }
    }

    private void getData() {

    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        super.dispatchTouchEvent(ev);
        return true;
    }
}
